/*
自己选择的路 ,跪着也要走完。朋友们 , 虽然这个世界日益浮躁起来,只
要能够为了当时纯粹的梦想和感动坚持努力下去 , 不管其它人怎么样,我
们也能够保持自己的本色走下去。
To the world , you will be a person , but to a person , you
will be the world .                               ——AKPower
*/
//作者:AKPower
//算法:
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
map<int, int> mp;
ll pre[maxn], val[maxn];
ll mx;
ll ans[maxn], del[maxn];
ll get(ll x)
{
    if (pre[x] == x)
        return x;
    return pre[x] = get(pre[x]);
}
int main()
{
    IOS;
    mx = 0;

    int n;
    cin >> n;
    memset(val, 0, sizeof(val));
    for (int i = 1; i <= n; i++)
        cin >> val[i];
    for (int i = 1; i <= n; i++)
        cin >> del[i];
    for (int i = 1; i <= n; i++)
        pre[i] = i;
    for (int i = n; i; i--)
    {
        ans[i] = mx;
        ll p = del[i];
        mp[p] = 1;
        if (p - 1 > 0 && mp[p - 1])
        {
            ll rt = get(p - 1);
            pre[rt] = p;
            val[p] += val[rt];
        }
        if (p + 1 <= n && mp[p + 1])
        {
            ll rt = get(p + 1);
            pre[rt] = p;
            val[p] += val[rt];
        }
        mx = max(val[p], mx);
        }

    for (int i = 1; i <= n; i++)
        cout << ans[i] << endl;
    getchar();
    getchar();
    return 0;
}
